﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class SysAdmin
    {

        public static void insert(SysAdminEntity admin)
        {
            DbHelper.ExecuteNonQuery("p_SysAdmin_insert", admin.Username,
                admin.Password,admin.Realname, admin.Email,admin.Mobile,admin.IsSalesperson,admin.Department);
        }

        public static void update(SysAdminEntity admin)
        {
            DbHelper.ExecuteNonQuery("[dbo].[p_SysAdmin_update]", admin.AdminId, admin.Username,
                admin.Password, admin.Realname, admin.Email, admin.Mobile,admin.IsSalesperson,admin.Disable,admin.IsAllowAccess,admin.Department);
        }

        public static void delete(int adminId)
        {
            DbHelper.ExecuteNonQuery("p_SysAdmin_delete", adminId);
        }
        public static int selectPsdhistory(int username, string uType)
        {
            object tmp = DbHelper.ExecuteScalar("p_PasswordHistory_select", username, uType);
            return Convert.ToInt32(tmp);
        }

        public static bool isExistUser(string username)
        {

            if (Convert.ToInt32(DbHelper.ExecuteScalar("p_SysAdmin_isExistUser", username)) == 1)
                return true;
            else
                return false;
        }

        public static SysAdminEntity login(string username, string password,int disablemin)
        {
            SysAdminEntity admin = new SysAdminEntity();
            DataRow dr = DbHelper.ExecuteDataRow("p_SysAdmin_login", username, password,disablemin);
            if (dr != null)
            {

                admin.AdminId = Convert.ToInt32(dr["AdminId"]);
                admin.Username = username;
                admin.Password = password;
                if(dr["StatusCode"]!=DBNull.Value)
                    admin.StatusCode = Convert.ToInt32(dr["StatusCode"]);
                if(dr["Errortimes"]!=DBNull.Value)
                    admin.ErrorPwdTimes = Convert.ToInt32(dr["Errortimes"]);
                if(dr["Errormin"]!=DBNull.Value)
                    admin.ErrorMin = Convert.ToInt32(dr["Errormin"]);

                admin.IsAllowAccess = Convert.ToBoolean(dr["isAllowAccess"]);
            }
            return admin;
        }

        public static SysAdminEntity getById(int adminId)
        {
            SysAdminEntity admin = new SysAdminEntity();
            DataRow dr = DbHelper.ExecuteDataRow("p_SysAdmin_getById", adminId);
            if (dr != null)
            {
                admin.AdminId = adminId;
                admin.Username = dr["username"].ToString();
                admin.Password = dr["password"].ToString();
                admin.Realname = dr["Realname"].ToString();
                admin.Email = dr["email"].ToString();
                admin.Mobile = dr["Mobile"].ToString();
                if (dr["LastDt"] != DBNull.Value)
                    admin.LastDt = Convert.ToDateTime(dr["LastDt"]);
                if (dr["CurrentDt"] != DBNull.Value)
                    admin.CurrentDt = Convert.ToDateTime(dr["CurrentDt"]);
                if (dr["Permission"] != DBNull.Value)
                    admin.Permission = dr["Permission"].ToString();
                admin.IsSalesperson = Convert.ToBoolean(dr["isSalesperson"]);
                if (dr["isAllowAccess"] != DBNull.Value)
                    admin.IsAllowAccess = Convert.ToBoolean(dr["isAllowAccess"]);
                bool isbool=false;
                bool.TryParse(dr["disable"].ToString(), out isbool);
                admin.Disable = isbool;
                if (dr["departmentId"] != DBNull.Value)
                    admin.Department = Convert.ToInt32(dr["departmentId"]);
            }
            return admin;
        }

      

        public static void updatePwd(SysAdminEntity admin)
        {
            DbHelper.ExecuteNonQuery("p_SysAdmin_updatePwd", admin.AdminId, admin.Password,admin.IP,admin.Oldpassword);
        }

        public static void updatePermission(int adminId, string permission)
        {
            DbHelper.ExecuteNonQuery("p_SysAdmin_updatePermission", adminId, permission);
        }
        public static DataTable listWithSalesperson()
        {
            return DbHelper.ExecuteDataTable("p_SysAdmin_listWithSalesperson");
        }

        public static void IsDisable(int adminid)
        {
            DbHelper.ExecuteNonQuery("p_SysAdmin_Disable", adminid);
        }

        public static void SendSMS(string phones, string content, int adminId, string url)
        {
            DbHelper.ExecuteNonQuery("[dbo].[p_SendSMS_Interface]", phones, content, adminId, url);
        }

        public static void chargeSeller(int categoryId, int adminId, int companyId)
        {
            DbHelper.ExecuteNonQuery("p_purchaseChargeSeller_insert", categoryId, adminId, companyId);
        }
        public static void chargeSellerDel(int pcsid)
        {
            DbHelper.ExecuteNonQuery("p_PurchaseChargeSeller_delete", pcsid);
        }
        public static DataTable getBydepartmentId(int dpId)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_SysAdmin_getBydepartmentId]",dpId);
        }

        public static void ErrorTimesToZero(int adminid)
        {
            DbHelper.ExecuteNonQuery("p_SysAdmin_ErrorTimesToZero", adminid);
        }
    }
}
